package com.xoms.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xoms.admin.controller.res.bean.RDepartmentBean;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.model.DepartmentBean;
import com.xoms.admin.persist.dao.DeptDao;
import com.xoms.admin.service.DeptService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.DateTimeUtil;

@Service
public class DeptServiceImpl implements DeptService {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	DeptDao deptImpl;

	@Override
	public RDepartmentBean edit(AccountBean account, int did, String name, int parentId, String desc, int isEnable) {
		DepartmentBean data = new DepartmentBean();
		boolean flag = false;
		data.setName(name);
		data.setRemark(desc);
		data.setParentId(parentId);
		data.setStatus(isEnable);

		if (did == -1) {// 新建
			// 新建时验证部门名称是否已存在账号
			DepartmentBean temp = deptImpl.selectOneByName(name);
			if (temp != null) {
				throw new ServiceException("dept_name_is_exist");
			}
	
			data.setAccountId(account.getAccid());
			data.setMember(0);
			flag = deptImpl.insert(data) > 0;
			if (!flag) {
				throw new ServiceException("database_insert_fail","部门信息");
			}
		} else {
			data.setDid(did);
			flag = deptImpl.update(data) > 0;// 更新数据库
			if (!flag) {
				throw new ServiceException("database_update_fail","部门信息");
			}
		}
		return parse(deptImpl.selectOneById(data.getDid()));
	}
	
	/**
	 * 格式化数据
	 * @param selectOneById
	 * @return
	 */
	private RDepartmentBean parse(DepartmentBean dept) {
		RDepartmentBean res = new RDepartmentBean();
		res.setDid(dept.getDid());
		res.setDesc(dept.getRemark());
		res.setIsEnable(dept.getStatus());
		res.setMember(dept.getMember());
		res.setName(dept.getName());
		res.setParentDept(dept.getParentDept());
		res.setParentId(dept.getParentId());
		res.setCreateTime(DateTimeUtil.formatTimestamp(dept.getCreateTime()));
		return res;
	}

	@Override
	public PageResult<RDepartmentBean> getDepts(AccountBean account, String key, int page, int pagesize) {
		PageResult<RDepartmentBean> result = new PageResult<RDepartmentBean>();
		List<RDepartmentBean> resList = new ArrayList<RDepartmentBean>();
		// 获取部门数量
		int count = deptImpl.selectCount(key);
		if (count > 0) {
			// 数量不为0时获取部门列表
			List<DepartmentBean> lists = deptImpl.selectList(key, page, pagesize);

			if (lists != null) {
				RDepartmentBean rb = null;
				for (DepartmentBean acc : lists) {
					rb = new RDepartmentBean();
					rb = parse(acc);
					resList.add(rb);
				}
			}
		}
		result.setData(resList);
		result.setCount(count);
		return result;
	}

	@Override
	public boolean enable(AccountBean account, int did) {
		if (deptImpl.updateStatus(did, 1) > 0) {// 更新状态
			return true;
		}

		return false;
	}

	@Override
	public boolean disable(AccountBean account, int did) {
		// 删除部门前判断该部门下有无员工，如有则无法删除
		int member = deptImpl.selectAccNum2Dept(did);
		if(member > 0) {
			throw new ServiceException("dept_has_member");
		}
		if (deptImpl.updateStatus(did, 0) > 0) {// 更新状态
			return true;
		}

		return false;
	}

	@Override
	public boolean del(AccountBean account, int did) {
		// 删除部门前判断该部门下有无员工，如有则无法删除
		int member = deptImpl.selectAccNum2Dept(did);
		if(member > 0) {
			throw new ServiceException("dept_has_member");
		}
		if (deptImpl.del(did) > 0) {// 删除部门
			return true;
		}
		return false;
	}

	
}
